package com.woniuxy.controller;


import com.woniuxy.dto.MyDto;
import com.woniuxy.service.AlipayService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
public class AliPayController {
    @Autowired
    private AlipayService alipayService;

    @ApiOperation(value = "支付接口",httpMethod = "GET",protocols = "HTTP",produces = "application/json",
            notes = "支付宝沙箱支付接口")
    @GetMapping("/pay")
    public void payMent(HttpServletResponse response, HttpServletRequest request, @ApiParam(required = true,name = "order_number",value = "订单编号")String order_number,  @ApiParam(required = true,name = "total_amount",value = "订单金额")String total_amount,@ApiParam(required = true,name = "subject",value = "商品名称") String subject ) {
        try {
            alipayService.aliPay(response,request,order_number,total_amount,subject);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *
     * @param out_biz_no  订单ID
     * @param payee_account 用户ID pxhclk2846@sandbox.com
     * @param payee_real_name  真实姓名   沙箱环境
     * @param amount 金额
     */

    @ApiOperation(value = "转账接口",protocols = "HTTP",produces = "application/json",
            notes = "支付宝沙箱转账接口")
    @PostMapping("/zhuan")
    public MyDto zhuan(@ApiParam(required = true,name = "out_biz_no",value = "订单ID")String out_biz_no, @ApiParam(required = true,name = "payee_account",value = "转账用户支付宝ID")String payee_account,@ApiParam(required = true,name = "payee_real_name",value = "转账用户支付宝名字") String payee_real_name,@ApiParam(required = true,name = "amount",value = "转账金额") String amount){
        System.out.println(amount);
       MyDto myDto=new MyDto();
        try {
            alipayService.zhuan(out_biz_no,payee_account,payee_real_name,amount);
            myDto.setCode(200);
            myDto.setMsg("转账成功");
        }catch (Exception e){
            e.printStackTrace();
            myDto.setCode(-200);
            myDto.setMsg("转账失败");
        }
        return  myDto;
    }
    @GetMapping("/test")
    public String test(){
        return "nacos测试";
    }
}
